package com.chujian.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chujian.dao.FoodListDao;
import com.chujian.dao.OrderDao;
import com.chujian.daoImpl.FoodListDaoImpl;
import com.chujian.daoImpl.OrderDaoImpl;
import com.chujian.jpush.Jdpush;
import com.chujian.model.FoodList;
import com.chujian.model.NotificationBean;
import com.chujian.model.Order;
import com.chujian.util.JsonUtil;
import com.google.gson.Gson;

/**
 * 退菜
 * Servlet implementation class FoodBackServlet
 */
@WebServlet("/FoodBackSellerServlet")
public class FoodBackSellerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FoodBackSellerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
//		获取客户端发送过来的数据
		String jsondata=request.getParameter("jsonString");
		System.out.println(jsondata);
		Gson gson=new Gson();
		FoodList foodList=JsonUtil.FoodListFromJson(jsondata);
	/*	FoodListDao dao=new FoodListDaoImpl();
		OrderDao orderDao=new OrderDaoImpl();
		boolean result =dao.updateFoodStatus(foodList.getFoodListId(), 1);
		Order order=orderDao.qeuryByOrderId(foodList.getOrderId());
		
		String notice=gson.toJson(new NotificationBean("OrderCurrentDetailSellerActivity", "退菜请求", "订单号："+foodList.getOrderId()+"的用户发起了退菜请求",gson.toJson(order)));
		Jdpush.sendPushAlias(notice, "退菜请求", "admin");
		if(result){
			out.write("foodBackRequestSuccess");
			System.out.println("foodBackRequestSuccess");
		}else {
			out.write("foodBackRequestFailed");
			System.out.println("foodBackRequestSuccess");
		}
		*/
		FoodListDao dao=new FoodListDaoImpl();
		OrderDao orderDao=new OrderDaoImpl();
		boolean result =dao.updateFoodStatus(foodList.getFoodListId(), 2);
//		boolean result=dao.deleteFoodList(foodList.getFoodListId());
		boolean result2=false;
		Order order=null;
		if(result){
			System.out.println("result=1");
			order=orderDao.qeuryByOrderId(foodList.getOrderId());
			float price=order.getTotalPrice()-foodList.getFoodPrice();
			order.setTotalPrice(price);
			
			result2=orderDao.updateTotalPrice(foodList.getOrderId(), price);
		}
		order.setFoodList(dao.qeuryByOrderId(order.getOrderId()));
		System.out.println(order.toString());
		if(result2){
			String notice=gson.toJson(new NotificationBean("0", "退菜成功", "店家同意了您"+"订单号："+order.getOrderId()+"的菜品："+foodList.getFoodName()+"的退菜请求",gson.toJson(order)));
			Jdpush.sendPushAlias(notice, "退菜成功", order.getUserPhone());
			
			out.write("foodBackSuccess");
			System.out.println("foodBackSuccess");
		}else {
			out.write("foodBackFailed");
			System.out.println("foodBackFailed");
		}
		
		
	}

}
